/**
 * @program: clms-second
 * @description:
 * @author: mitu_echo
 * @create: 2021-07-25 10:16
 **/

package com.second.lease.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.second.common.pojo.R;
import com.second.lease.dao.CarBrandDao;
import com.second.lease.pojo.entity.CarBrand;
import com.second.lease.pojo.vo.CarBrandVO;
import com.second.lease.service.CarBrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarBrandServiceImpl implements CarBrandService {

    @Autowired
    private CarBrandDao carBrandDao;

    @Override
    public R showCarBrand(CarBrandVO carBrandVO) {

        if (carBrandVO.getPageno() != null) {
            QueryWrapper queryWrapper = new QueryWrapper();
            PageHelper.startPage(carBrandVO.getPageno(),carBrandVO.getPagesize());
            if (StringUtils.isNotEmpty(carBrandVO.getName())) {
                queryWrapper.like("name",carBrandVO.getName());
            }
            List carBrandList = carBrandDao.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(carBrandList);
            return R.success().putListData(carBrandList).putObject(pageInfo);
        }

        List carBrandList = carBrandDao.selectList(null);
        return R.success().putListData(carBrandList);
    }

    @Override
    public R addCarBrand(CarBrand carBrand) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",carBrand.getName());
        CarBrand c = carBrandDao.selectOne(queryWrapper);
        if ( c == null) {
            int i = carBrandDao.insert(carBrand);
            if (i>0) {
                return R.success();
            }
            return R.error();
        }
        return R.error();
    }

    @Override
    public R updayeCarBrand(CarBrand carBrand) {

        int i = carBrandDao.updateById(carBrand);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R deleteCarBrand(Integer id) {

        int i = carBrandDao.deleteById(id);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R updateStatus(Integer id, Integer status) {

        CarBrand carBrand = new CarBrand();
        carBrand.setId(id);
        carBrand.setStatus(status);
        int i = carBrandDao.updateById(carBrand);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }
}
